<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var findRestaurant = function (list1, list2) {
      const index = new Map();
      for (let i = 0; i < list1.length; i++) {
        index.set(list1[i], i);
      }
      const ret = [];
      let indexSum = Number.MAX_VALUE;
      for (let i = 0; i < list2.length; i++) {
        if (index.has(list2[i])) {
          const j = index.get(list2[i]);
          if (i + j < indexSum) {
            ret.length = 0;
            ret.push(list2[i]);
            indexSum = i + j;
          } else if (i + j == indexSum) {
            ret.push(list2[i]);
          }
        }
      }
      return ret;
    };
    console.log(findRestaurant(
      list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"],
      list2 = ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
    ))
  </script>
</body>

</html>